/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.JLabel;
import org.aswing.UIManager;

import fever.utils.Stringifier;

/**
 * Label act as a button.
 *  
 * @author Romain Ecarnot
 */
class fvaswing.components.datechooser.FvDayCell extends JLabel
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------

	private var _selected : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
		
	public function FvDayCell(text, icon, horizontalAlignment : Number)
	{
		super( text, icon, horizontalAlignment );
		
		normal();
		
		_selected = false;
		
		setFocusable( true );
		setOpaque( true );
		setUseHandCursor( false );
	}
	
	/**
	 * Returns date value in field.
	 * 
	 * @return Number
	 */
	public function getDate(  ) : Number
	{
		var s : String = getText();
		
		if( s.length > 0 ) return Number( s );
		return null;
	}
	
	/**
	 * Enable or disable the component.
	 */
	public function setEnabled( b : Boolean ) : Void
	{
		if( b != isEnabled() )
		{
			_applyStyle( 'List.foreground', 'List.background' );
			repaint();
		}
		
		super.setEnabled(b);
	}
	
	public function highlight() : Void
	{
		_applyStyle( 'List.selectionForeground', 'List.selectionBackground' );
	}
	
	public function normal() : Void
	{
		_selected = false;
		_applyStyle( 'List.foreground', 'List.background' );
	}
	
	public function select() : Void
	{
		_selected = true;
		
		highlight();	
	}
	
	public function setSelected( b : Boolean ) : Void
	{
		if( b ) select();
		else normal();	
	}
	
	public function isSelected() : Boolean
	{
		return _selected;	
	}
	
	public function invert() : Void
	{
		setSelected( !_selected );	
	}
	
	public function disabled() : Void
	{
		_applyStyle( 'List.foreground', 'List.background' );
		repaint();
		
		super.setEnabled( false );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _applyStyle( foreColor : String, backColor : String ) : Void
	{
		setForeground( UIManager.getColor( foreColor ) );
		setBackground( UIManager.getColor( backColor ) );
		
		repaint();
	}
	
	private function __onRelease() : Void
	{
		super.__onRelease();
		
		dispatchEvent( ON_ACT, createEventObj( ON_ACT ) );
	}
}